package cn.zt.controller;

import cn.zt.dto.ResultDTO;
import cn.zt.dto.UserDto;
import cn.zt.model.User;
import cn.zt.service.UserService;
import cn.zt.util.MD5Util;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @Author: zt
 * @Date: 2020/5/25 9:27
 */
@RestController
@RequestMapping("/registLogin")
@Api(value = "registLogin", description = "注册和登录接口")
public class RegistLoginController extends BaiscController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation("用户注册")
    @GetMapping("/regist")
    public ResultDTO regist(User user) {

        User user1 = new User();
        try {

            //判断用户名和密码是否为空
            if ("".equals(user.getName()) || "".equals(user.getPassword()) || user.getName() == null || user.getPassword() == null) {
                return ResultDTO.isFail().data("用户名和密码不能为空");
            }
            //判断用户是否存在
            boolean userNameIsExist = userService.queryUserNameIsExist(user.getName());
            SimpleDateFormat sdd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //当用户不存在  注册用户
            if (!userNameIsExist) {
                user1.setName(user.getName());
                user1.setNickName(user.getName());
                user1.setPassword(MD5Util.string2MD5(user.getPassword()));
                user1.setReceiveCounts(0);
                user1.setFansCounts(0);
                user1.setFollowCounts(0);
                user1.setRegistTime(sdd.format(new Date()));
                userService.saveUser(user1);
            } else {
                //当用户存在时
                return ResultDTO.isFail().data("用户名已存在");
            }
        } catch (Exception e) {
            return ResultDTO.isFail(e).data("发生异常");
        }
        user1.setPassword("");
        UserDto userDto = setUserRedisSessionToken(user1);
        return ResultDTO.isOk().data(userDto);

    }

    public UserDto setUserRedisSessionToken(User userModel){
        String uniqueToken = UUID.randomUUID().toString();
        redis.set(USER_REDIS_SESSION+":"+userModel.getId(),uniqueToken,1000*60*30 );
        UserDto userDto = new UserDto();
        BeanUtils.copyProperties(userModel,userDto);
        userDto.setUserToken(uniqueToken);
        return userDto;
    }



    @ApiOperation(value = "用户登录", notes = "登录接口")
    @PostMapping("/login")
    public ResultDTO login(@RequestBody User user) {

        User userResult = new User();
        try {
            String name = user.getName();
            String password = user.getPassword();
            //判断用户名密码是否为空
            if (name.isEmpty() || password.isEmpty()){
                return ResultDTO.isFail().data("用户名和密码不能为空");
            }
            //查询此用户是否存在
            String passwords = MD5Util.string2MD5(password);
            userResult =  userService.queryUserForLogin(name,passwords);
            if (userResult != null){
                userResult.setPassword("");//返回前端数据把密码设置为空  不让前端看见
                UserDto userDto = setUserRedisSessionToken(userResult);
                return ResultDTO.isOk().data(userDto).msg("登录成功");
            }
        } catch (Exception e) {
            return ResultDTO.isFail(e);

        }

        return ResultDTO.isFail().msg("用户名和密码不正确") ;

    }
    @ApiOperation(value = "用户注销", notes = "注销接口")
    @PostMapping("/logout")
    public ResultDTO logout(@RequestBody User user) {

        User userResult = new User();
        try {
            Thread.sleep(3000);
            String name = user.getName();
            String password = user.getPassword();



            //判断用户名密码是否为空
            if (name.isEmpty() || password.isEmpty()){
                return ResultDTO.isFail().data("用户名和密码不能为空");
            }
            //查询此用户是否存在
            String passwords = MD5Util.string2MD5(password);
            userResult =  userService.queryUserForLogin(name,passwords);
            if (userResult != null){

                return ResultDTO.isOk().data(userResult).msg("登录成功");
            }
        } catch (Exception e) {
            return ResultDTO.isFail(e);
        }

        return ResultDTO.isFail().msg("用户名和密码不正确") ;

    }
}